module.exports = function ($scope, $resource, $modalInstance, bean, toaster, FileUploader) {
    $scope.pageInfo = {
        asrot: 0,
        product: {}
    }
    $scope.bean = bean;
    $scope.load = function () {
        $resource('/api/as/mc/shopcolumn/findColumnProductList', {}, {}).save($scope.bean, function (res) {
            if (res.errcode === 0) {
                $scope.columnProductList = res.data;
                for(var i in $scope.columnProductList){
                  $scope.columnProductList[i].update_state = true;
              }
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
        $resource('/api/as/mc/shopcolumn/noColumnProductList', {}, {}).save($scope.bean, function (res) {
            if (res.errcode === 0) {
                $scope.noColumnProductList = res.data;
                if (!$scope.noColumnProductList.length) {
                    $scope.noColumnProductList.push({
                        product_name: '--没有未添加的产品--',
                        product_code: 0
                    })
                }
                $scope.pageInfo.product = $scope.noColumnProductList[0]
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
    $scope.load();

    //添加栏目下产品
    $scope.save = function () {
        if (!$scope.pageInfo.product.product_code) {
            toaster.warning({ title: '', body: '请选择产品' })
            return false;
        }
        let para = {
            product_code: $scope.pageInfo.product.product_code,
            column_code: $scope.bean.column_code,
            asrot: $scope.pageInfo.asrot || 0
        }
        $resource('/api/as/mc/shopcolumn/saveColumnProduct', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $scope.load();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //删除栏目下产品
    $scope.delete = function (index) {
        let para = {
            column_code: $scope.bean.column_code,
            product_code: $scope.columnProductList[index].product_code
        }
        $resource('/api/as/mc/shopcolumn/deleteColumnProduct', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $scope.load();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //关闭
    $scope.ok = function () {
        $modalInstance.close();
    }

    //取消
    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }
   
       //修改
       $scope.update = function (info) {
        info.update_state = false;
    }

    //保存
    $scope.saveone = function (info) {
        
        $resource('/api/as/mc/shopcolumn/updateProductAsort', {}, {}).save(info, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $scope.load();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
};